jQuery(function ($) {
    var user = JSON.parse(localStorage.getItem('user_info'));
    // if (!user) {
    //     window.location.href = "/login/login.html";
    // }
    // var userId = user.id;
    var userId = 1179;
    var addressList = {
        "UserAddress[]": {
            "UserAddress": {
                "user_id": userId
            }
        }
    };
    if (userId) {
        send_ajax('post', '/get', addressList, function callback(data) {
            if (data.code == 200 && data["UserAddress[]"]) {
                addrs = data["UserAddress[]"];
                for (var i = 0; i < addrs.length; i++) {
                    joint_address(addrs[i]);
                }
            } else {
                return;
            };
        });
        function joint_address(address) {
            var area_ids = {
                "Area[]": {
                    "count": 10,
                    "Area": {
                        "use_flag": 1,
                        "id{}": [address.province, address.city, address.district, address.street]
                    }
                }
            };
            send_ajax('post', '/get', area_ids, function callback(data) {
                if (data.code == 200&&data["Area[]"]) {
                    //修改主数据
                    var addr_info = data["Area[]"];
                    address.province_obj = addr_info[0];
                    address.city_obj = addr_info[1];
                    address.district_obj = addr_info[2];
                    address.street_obj = addr_info[3];
                }
                $('.pur_addr').append('<option value="' + address.id + '">'+((address.province_obj ? address.province_obj.name : '') + (address.city_obj ? address.city_obj.name : '') + (address.district_obj ? address.district_obj.name : '') + (address.street_obj != undefined ? address.street_obj.name : "")+(address.location?address.location:''))+'</option>')
            });
        };
        //采购分类，加单位
        send_ajax('get', '/api/order/getRequestCat', '', function callback(data) {
           if(data.code==200){
                var requestCat = data.data.requestCat;//分类
                var unit = data.data.unit;//单位
                var requestHtml="";
                var unitHtml="";
                for(var i = 0;i<requestCat.length;i++){
                    var a_requestCat = requestCat[i];
                    requestHtml+='<option value="'+a_requestCat.id+'">'+a_requestCat.name+'</option>'
                };
                $('.pur_type').html(requestHtml);
                for(var n =0;n<unit.length;n++){
                    var a_unit = unit[n];
                    unitHtml+='<option value="'+a_unit.key+'">'+a_unit.value+'</option>' 
                };
                $('.pur_unit').append(unitHtml);
                $(".pur_num").bind("input propertychange",function(){
                   var jumpNum =  $('.pur_num').val();
                   $(".pur_num").val(jumpNum.replace(/[^\d]/g,''));
               });
           }
        
           $('.pur_btn').click(function(){
                var requestCat_id = $(".pur_type option:selected").val();
                var count = $('.pur_num').val();
                var addrId = $(".pur_addr option:selected").val();
                var unit = $(".pur_unit option:selected").val();
                var title =$('.pur_title').val();
                var content = $('.xheditor').val();
                var province = $('.province').val();
                var city = $('.city').val();
                var area = $('.area').val();
                var town = $('.town').val();
                if(unit==''){
                    alert('请选择数量单位！');
                    return;
                };
                if(count==''){
                    alert('请输入采购数量！');
                    return;
                }
                if(addrId==''){
                    alert('请选择收货地址！');
                    return;
                }
                if(title==''){
                    alert('请输入采购标题！');
                    return;
                }
                if (province != '' && city != '' && area != '' && title !='' ) {
                    var param={
                        "address_id": addrId,
                        "city": cityId,
                        "count": count,
                        "district": areaId,
                        "province": provinceId,
                        "requestCat_id": requestCat_id,
                        "street": townId ? townId:0,
                        "title": title,
                        "unit": unit,
                        "content":content?content:'',
                        "user_id": userId
                    };
                    send_ajax('post', '/api/order/addRequest', param, function callback(data) {
                        if(data.code==200){
                           alert('发布成功');
                           window.location.reload();
                        }
                    });
                }else{

                }
           })
        });
    };
});